// pages/login/basicInfo/index.js
import api  from "../../../api/index"
import constant from "../../../utils/constant"


var app = getApp();
const date = new Date()


Page({

  /**
   * 页面的初始数据
   */
  data: {
    CustomHeight: app.globalData.windowHeight,
    maxBirtyday: date.getFullYear()-12 + '-' + date.getMonth() + 1 + '-' + date.getDate(),
    num: 0,
    numList: [],
    nickname: '',
    avatar: '',
    sex: '',
    birthday: '',
    lunar: '',
    star: '',
    zodiac: '',
    areas: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
		let numList = []
		numList.push({ name: '昵称' })
		numList.push({ name: '性别' })
		numList.push({ name: '生日' })
		numList.push({ name: '居所' })

		this.setData({
			numList: numList
		})

	},
	
	// bindNickname(e) {
	// 	this.setData({
	// 		nickname: e.detial.value
	// 	})
	// },

	numSteps() {
		if(this.data.num==0 && wx.$isNull(this.data.nickname)){
			return wx.$msg('起个好听的名字吧')
		}
		else if(this.data.num==1 && wx.$isNull(this.data.sex)){
			return wx.$msg('别丢了性别呀')					
		} 
		else if(this.data.num==2 && wx.$isNull(this.data.birthday)){
			return wx.$msg('您的生日是?')
		}
		else if(this.data.num==3 && wx.$isNull(this.data.areas)){
			return wx.$msg('别忘了告诉我你在哪?')
		}
		else {
      this.setData({
        num: this.data.num == this.data.numList.length - 1 ? 0 : this.data.num + 1
      })
		}
  },
  goNum(e){
    let n = e.currentTarget.dataset.index
    if(n<this.data.num){
      this.setData({
        num: n
      })
    }

  },
	handleChooseAvatar(e){
    if(!wx.$isNull(this.data.avatar)){
      api.common.delFile(this.data.avatar)
    }
    api.common.uploadAvatarFile(e.detail.avatarUrl).then((res)=>{
      this.setData({
        avatar: res
      })
    })
	},
  handleSex(e){
    this.setData({
      sex: e.currentTarget.dataset.sex
    })
  },
  handleDateChange(e){
    this.setData({
      birthday: e.detail.date,
      lunar: e.detail.lunar,
      star: e.detail.star,
      zodiac: e.detail.zodiac
    })
  },
  handleAreaChange(e){
    this.setData({
      areas: e.detail.name
    })
  },
  handleEnd(){
    this.numSteps()

    const info = {
      nickName: this.data.nickname,
      avatar: this.data.avatar,
      sex: this.data.sex,
      birthday: this.data.birthday,
      lunar: this.data.lunar,
      star: this.data.star,
      zodiac: this.data.zodiac,
      lifeProvince: this.data.areas[0],
      lifeCity: this.data.areas[1],
      lifeArea: this.data.areas[2]
    }
    api.user.setProfile(info).then((res)=>{
      wx.setStorageSync(constant.USER_INFO, info)
      wx.reLaunch({
        url: '/pages/user/index',
      })
    })
  },




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})